import { _decorator, Component, director, instantiate, math, Node, Prefab, UITransform, Vec3 } from 'cc';
import { Enemy } from './Enemy/Enemy';
const { ccclass, property } = _decorator;

@ccclass('EnemyManager')
export class EnemyManager extends Component {

    private static _instance: EnemyManager = null;
    // 敌人预制体
    @property({ type: Prefab })
    public enemyPrefab: Prefab = null;

    @property({ type: Prefab })
    public bossPrefab: Prefab = null; // 新增boss预制体

    @property({ type: Node })
    public enemyLayer: Node = null;

    @property({ type: Node })
    public winWnd: Node = null;
    public enemies: Node[] = []; // 存储所有敌人节点

    @property
    public spawnInterval: number = 1;

    @property
    public spawnCount: number = 2; // 每次生成的敌人数量

    private maxEnemyCount: number = 20; // 最大敌人数量
    private bossSpawned: boolean = false; // 标记boss是否已生成

    public static get instance(): EnemyManager {
        if (!this._instance) {
        }
        return this._instance!;
    }

    onLoad() {
        if (EnemyManager._instance) {
            return;
        }
        EnemyManager._instance = this;
    }
    start() {
        this.schedule(this.spawnEnemy, this.spawnInterval);
    }

    update(deltaTime: number) {
        // 检查是否所有敌人都被消灭
        if (this.enemyLayer.children.length === 0 && this.bossSpawned) {
            this.gameWin();
        }
    }

    // 游戏胜利逻辑
    gameWin(): void {
        this.winWnd.active = true;
        director.pause();
    }

    // 生成敌人
    spawnEnemy(): void {
        // 如果当前敌人数量达到最大值
        if (this.enemies.length >= this.maxEnemyCount) {
            // 如果boss还未生成，则生成boss
            if (!this.bossSpawned && this.bossPrefab) {
                const boss = instantiate(this.bossPrefab);
                const pos = this.getRandomPositionInArea();
                boss.setPosition(pos);
                this.node.addChild(boss);
                this.enemies.push(boss);
                this.bossSpawned = true;
            }
            return;
        }

        if (!this.enemyPrefab) {
            console.error('enemyPrefab 未设置！');
            return;
        }

        // 一次性生成多个敌人
        for (let i = 0; i < this.spawnCount; i++) {
            if (this.enemies.length >= this.maxEnemyCount) break;

            const enemy = instantiate(this.enemyPrefab);
            const pos = this.getRandomPositionInArea();
            enemy.setPosition(pos);
            this.node.addChild(enemy);
            this.enemies.push(enemy);
        }
    }

    // 获取随机生成的位置
    getRandomPositionInArea(): Vec3 {
        if (!this.enemyLayer) {
            // 如果没有设置 spawnArea，则在一个默认区域生成
            return new Vec3(math.randomRange(-100, 100), math.randomRange(-100, 100), 0);
        }
        const transform = this.enemyLayer.getComponent(UITransform);
        const areaPos = this.enemyLayer.worldPosition;
        const width = transform.width;
        const height = transform.height;
        // 在区域内随机生成一个坐标
        const randomX = math.randomRange(areaPos.x - width / 2, areaPos.x + width / 2);
        const randomY = math.randomRange(areaPos.y - height / 2, areaPos.y + height / 2);
        return new Vec3(randomX, randomY, 0);
    }

}
